import { IsDefined, IsInt, IsNotEmpty, IsString } from "class-validator";
import { BaseEntity } from "../../../common/BaseEntity";
import { TStatus } from "../../../common/CommonType";
import { Column, Entity, JoinColumn, JoinTable, ManyToMany, OneToOne, PrimaryGeneratedColumn } from "typeorm";
import SysRole from "../role/Entity";
import SysMenuMetaEntity from "../menuMeta/Entity";
import SysMenuPermissionEntity from "../menuPermission/Entity";

@Entity({
  name: "sys_menu",
})
export default class SysMenuEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  public id: number;

  @IsString()
  @Column({
    nullable: true,
  })
  public name: string;

  /**
   * 上级菜单id
   */
  @IsInt()
  @IsNotEmpty()
  @IsDefined()
  @Column({
    nullable: true,
  })
  public parentId: number;

  /**
   * 路由 path
   */
  @IsString()
  @Column({
    nullable: true,
  })
  public path: string;

  /**
   * 组件路径文件路径
   */
  @IsString()
  @Column({
    nullable: true,
  })
  public component: string;

  /**
   * 菜单类型
   */
  @IsInt()
  @Column({
    nullable: true,
  })
  public type: number;

  /**
   * 菜单状态
   */
  @IsInt()
  @IsNotEmpty()
  @IsDefined()
  @Column({
    nullable: true,
  })
  public status: TStatus;

  /**
   * 重定向
   */
  @IsString()
  @Column({
    nullable: true,
  })
  redirect: string;

  /**
   * 与角色表关联
   */
  @ManyToMany(() => SysRole, (role) => role.menus)
  roles: SysRole[];

  /**
   * 与meta表关联
   */
  @OneToOne(() => SysMenuMetaEntity, (SysMenuMetaEntity) => SysMenuMetaEntity.menu)
  @JoinColumn()
  meta: SysMenuMetaEntity;

  /**
   * 与permission表关联
   */
  @ManyToMany(() => SysMenuPermissionEntity, (SysMenuPermissionEntity) => SysMenuPermissionEntity.menu)
  @JoinTable()
  permission: SysMenuPermissionEntity[];

  permissionLabels: string[];
  parentName: string;

  public static transform(plainObject: object): SysMenuEntity {
    return super.baseTransform(SysMenuEntity, plainObject);
  }
}
